<?php


namespace app\admin\model;


use think\Model;

class Qu extends Model
{
    protected $autoWriteTimestamp = true;
    public static function list($text,$quId,$staffNo)
    {
        $sql = Qu::field("*");
        if($text){
            $sql->where("text","%$text%");
        }
        if($quId){
            $sql->where("qu_id",$quId);
        }
        if($staffNo){
            $sql->where("staff_no",$staffNo);
        }

        $c = $sql->count();
        $l = $sql->select();

        return listDate($l,$c);
    }

    public static function add($title,$text,$quId,$staffNo,$staffName)
    {
        $qu = Qu::field("id")->where("title",$title)->find();
        if($qu){
            return "数据已存在~";
        }
        Qu::create(["title"=>$title,"text"=>$text,"qu_id"=>$quId,"staff_no"=>$staffNo,"staff_name"=>$staffName]);
        return "";
    }

    public static function up($title,$text,$quId,$id)
    {
        $qu = Qu::field("id")->where("id",$id)->find();
        if(!$qu){
            return "数据不存在~";
        }
        Qu::where("id",$id)->data(["title"=>$title,"text"=>$text,"qu_id"=>$quId])->update();
        return "";
    }

    public static function del($id)
    {
        $qu = Qu::field("id")->where("id",$id)->find();
        if(!$qu){
            return "数据不存在~";
        }
        Qu::where("id",$id)->delete();
        return "";
    }

    public static function ifQu($qu)
    {
        return Qu::where("title","like","%$qu%")
            ->whereOr("text","like","%$qu%")
            ->value("text");
    }
}